package com.bestalbum.net{
	import com.bestalbum.ui.LoaderOne;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.events.DataEvent;
	
	public class Service extends EventDispatcher{
		
		/*
		http://beta.bestalbum.ru/service.php?action=getClientProfile&param=123
		*/
		public static const SERVICE_METHOD 				= "POST";
		
		public static const ACTION_INIT_INTERFACE 		= "initInterface";
		public static const ACTION_GET_CLIENT_PROFILE 	= "getClientProfile";
		
		public static const SERVICE_DOMAINE 			= "http://beta.bestalbum.ru";
		public static const SERVICE_ACCESS_POINT 		= "service.php";
		public static const SERVICE_URL 				= SERVICE_DOMAINE + "/" + SERVICE_ACCESS_POINT;
		public static const EVENT_EXCHANGE 				= "exchange";
		
		private var urlRequest:URLRequest;
		private var loader:URLLoader;
		private var mainStage:MovieClip;
		private var total:Sprite;
		private var loaded:Sprite;
		private var loaderOne:LoaderOne;
		private var serviceParameters:URLVariables;
		
		public function Service(mStage:MovieClip){
			urlRequest = new URLRequest(SERVICE_URL);
			urlRequest.method = SERVICE_METHOD;
			mainStage = mStage;
		}
		
		public function load(path:String){
			loader = new URLLoader(); 
			loader.dataFormat = URLLoaderDataFormat.BINARY; 
			loader.load(new URLRequest(path)); 
		}
		
		public function getData():Object{
			return loader.data;
		}
		
		private function onLoadComplete(event:Event):void{
			var e:DataEvent = new DataEvent(Event.COMPLETE, false, false, loader.data);
            dispatchEvent(e);
		}
		
		public function dataExchange(action:String){	
			urlRequest.data = getServiceParameters(action);
			loader = new URLLoader();
			addProgressBar();
			addListeners();
			loader.load(urlRequest);
		}
		
		private function addProgressBar(){
			//sprite that represents the total amount of data that we need to load
			total = new Sprite();
			total.graphics.beginFill(0xff0000, 1);
			total.graphics.drawRect(0, 0, 600, 3);
			total.graphics.endFill();
			mainStage.addChild(total);
			total.y = 420;
			total.x = 10;
			//sprite that represents the amount loaded
			loaded = new Sprite();
			mainStage.addChild(loaded);
			loaded.y = 420;
			loaded.x = 10;
		}
		
		private function activatedListener(event:Event):void{
			trace("Service::activatedListener() activated " + loader.bytesLoaded + "but nothing loaded yet ");
			mainStage.addText("Service::activatedListener()", true);
		}
	
		private function progressListener(event:Event):void{
		 	trace("Service::progressListener() we're in progress, we've loaded " + loader.bytesLoaded + " out of " + loader.bytesTotal + " bytes ");
			mainStage.addText("Service::progressListener() we're in progress, we've loaded " + loader.bytesLoaded + " out of " + loader.bytesTotal + " bytes ", false);
			if(!loaderOne){
				loaderOne = new LoaderOne();
				mainStage.addChild(loaderOne);
			}
			var bt:Number = loader.bytesTotal;
			if(bt == 0) bt = 1640292;
			var amount:Number = (loader.bytesLoaded / bt) * 600;
			loaded.graphics.clear();
			loaded.graphics.beginFill(0x00ff00, 1);
			loaded.graphics.drawRect(0, 0, amount, 3);
			loaded.graphics.endFill();
		}
		
		private function completeListener(event:Event):void{
			trace("Service::completeListener() all done loading and here's the xml file we loaded ");
			mainStage.addText("Service::completeListener()", true);
			removeListeners();
			mainStage.removeChild(loaderOne);
			mainStage.removeChild(total);
			mainStage.removeChild(loaded);
			var myXML:XML = XML(loader.data);
			// mainStage.addText(myXML.toString(), true);
			// mainStage.addText("Service::completeListener() el=" + myXML.call.profile.bigData[15200], true);
			// mainStage.addText("Service::completeListener() id=" + myXML.call.profile.bigData[15200].@id, true);
			// trace(myXML);
            var e:DataEvent = new DataEvent(Service.EVENT_EXCHANGE, false, false, myXML.call.profile);
            dispatchEvent(e);
		}
			
		private function getServiceParameters(action:String):URLVariables{
			serviceParameters = new URLVariables();
			serviceParameters.action = action;
			switch(action){
				case Service.ACTION_INIT_INTERFACE:
					serviceParameters.param = 111;
				break;
				case Service.ACTION_GET_CLIENT_PROFILE:
					serviceParameters.param = 1234;
				break;
			}
			return serviceParameters;
		}
		
		private function errorListener(event:Event):void{
		 	trace("Service::errorListener() ERROR: " + event.toString());
		 	mainStage.addText("Service::errorListener()", true);
		 	removeListeners();
		}
		
		private function addListeners(){
			loader.addEventListener(Event.ACTIVATE, activatedListener);
			loader.addEventListener(Event.COMPLETE, completeListener);
			loader.addEventListener(IOErrorEvent.IO_ERROR, errorListener);
			loader.addEventListener(ProgressEvent.PROGRESS, progressListener);
		}
		
		private function removeListeners(){
			loader.removeEventListener(Event.ACTIVATE, activatedListener);
			loader.removeEventListener(Event.COMPLETE, completeListener);
			loader.removeEventListener(IOErrorEvent.IO_ERROR, errorListener);
			loader.removeEventListener(ProgressEvent.PROGRESS, progressListener);
		}

	}
}